//
//  ViewController.m
//  collectionView实现瀑布流
//
//  Created by Hello Cai on 2018/7/25.
//  Copyright © 2018年 Hello Cai. All rights reserved.
//

#import "ViewController.h"
#import "GHLineLayout.h"    //继承自UICollectionViewFlowLayout
#import "GHGridLayout.h"    //继承自UICollectionViewLayout
#import "GHPhotoCell.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation ViewController

static NSString *photoCellID = @"photoCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self setupFlowLayout];
    
//    [self setupCustomGridLayout];
}

//横向居中照片流
-(void)setupFlowLayout{
    //UICollectionViewFlowLayout管理布局，默认支持上下滚动，像水一样，这行装满了就流下去
    GHLineLayout *flowLayout = [[GHLineLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 100);
    
    CGFloat collectionW = self.view.frame.size.width;
    CGFloat collectionH = 200;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 150, collectionW, collectionH) collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([GHPhotoCell class]) bundle:nil] forCellWithReuseIdentifier:photoCellID];
}


-(void)setupCustomGridLayout{
    GHGridLayout *gridLayout = [[GHGridLayout alloc] init];
    //原始布局没有itemSize属性
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:gridLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([GHPhotoCell class]) bundle:nil] forCellWithReuseIdentifier:photoCellID];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 20;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GHPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:photoCellID forIndexPath:indexPath];
    cell.imageName = [NSString stringWithFormat:@"%zd", indexPath.item + 1];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了%zd",indexPath.item);
}
@end
